<?php
namespace app\atxunda\model;
use think\Model;
use think\Db;
use think\Input;
use think\Cache;
use think\Cookie;
use app\index\model\KnowUserVip;
use app\index\model\traits\ModelHasTime;
use app\index\model\KnowUserNav as KnowUserNavIndex;

class KnowUser extends Model
{
	use ModelHasTime;

	public  function userAlls($condition,$admin){
		$start_time = $condition['start_time'];
		$end_time = $condition['end_time'];
		$phone = $condition['phone'];
		$action = $condition['action'];
		$where = 1;
		if(!empty($start_time)){
			$start_times = strtotime($start_time);
			$where .= " and UNIX_TIMESTAMP(register_time)>=$start_times";
		}
		if(!empty($end_time)){
			$end_times = strtotime($end_time);
			$where .= " and UNIX_TIMESTAMP(register_time)<=$end_times";
		}
		if(!empty($phone)){
			$phone = trim($phone);
			$phone = encrypts($phone,config('sha256'));
			$where .= " and (know_phone='$phone' or know_email='$phone')";
		}
		if(!empty($from)){
			$where .= " and `from` is not null";
		}
		if(is_numeric($action)){
			$where_action = $action-1;
			$where .= " and action=$where_action";
		}
		$pageParam['query'] = $condition;
		/*$navIds = Db::table('course')
						->where('is_show', 1)->column('nav_id');
		$courseIds = Db::table('course')
						->where('course.nav_id','in',$navIds)
						->where('course.is_show', 1)
						->where('type.is_show', 1)
						->where('s_show', 1)
						->join('specific','specific.s_id=course.s_id')
						->join('type','type.type_id=course.type_id')
						->column('course_id');
		$courseIds = implode(',', $courseIds);*/
		$order = 'register_time desc';
		if ($condition['order'] == 'record') $order = 'records desc,register_time desc';
		$user_data = self::field('*,(select sum(record) from know_user_record r where know_user_id=user_id) as records')
				->order($order)
				->where($where)
				->paginate(30,false, $pageParam);
		return $user_data;
	}



	public function disposeExpireTime($user){
		$lockData = Db::table('know_user_lock')->where("user_id",$user['know_user_id'])->where('is_locking',1)->find();
		$year = $month = $day = $y = $m = $d = 0;
		if($lockData) list($y,$m,$d) = DiffDateMonth(date('Y-m-d',$lockData['start_time']),date('Y-m-d',$lockData['end_time']));
		if($user['is_vip'] != 0){
			$year = 10;
			$year += $y;
			$month = $m;$day = $d;
			$time = strtotime($user['vip_time']);
			$expire_time = date('Y-m-d H:i:s',strtotime("+".$day." day +".$month." month +".$year." year",$time));
			self::where('know_user_id',$user['know_user_id'])->update(['expired_time'=>$expire_time]);
		}else{
			$vipData = KnowUserNavIndex::getUserValid(['user_id'=>$user['know_user_id'],'vip_id'=>0,'valid'=>'yes']);
			$userVip = KnowUserVip::getUserVip($user['know_user_id']);
			$navIds = [];
			if($vipData) {
				$course = [];
				$year = 2+(count($vipData)-1);
				if($userVip['type'] != 4 && $userVip['year']) $year = $userVip['year'];
				$year += $y;
				$month = $m;$day = $d;
				foreach ($vipData as $key => $vip) {
					$time = strtotime($vip['time']);
					$expire_time = date('Y-m-d H:i:s',strtotime("+".$day." day +".$month." month +".$year." year",$time));
					KnowUserNav::where(['user_id'=>$user['know_user_id'],'nav_id'=>$vip['nav_id']])->update(['expired_time'=>$expire_time]);
				}
			}
			if(!$vipData){
				$vipData = KnowUserNavIndex::getUserValid(['user_id'=>$user['know_user_id'],'vip_id'=>['neq',0],'valid'=>'yes']);
				$year = $userVip['year'];
				if($vipData) foreach ($vipData as $key => $vip) {
					$time = strtotime($vip['time']);
					$expire_time = date('Y-m-d H:i:s',strtotime("+".$day." day +".$month." month +".$year." year",$time));
					KnowUserNav::where(['user_id'=>$user['know_user_id'],'nav_id'=>$vip['nav_id']])->update(['expired_time'=>$expire_time]);
				}
			}
		}
	}
}